package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppGuard;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class AppGuardDao extends BaseDao<AppGuard> {

    @Autowired
    private AppSetupDao appSetupDao;
    @Autowired
    private AppUserDao appUserDao;

    /**
     * 获取用户被守护的信息
     * @param beiUserId
     * @return
     */
    public AppGuard getGuardInfo(Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = " select * from app_guard where beiUserId=? and isGuard=1";
        list.add(beiUserId);
        return this.queryObj(sql,list,AppGuard.class);
    }


    public List<AppGuard> getMyGuardInfo(Integer userId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_guard where userId=? and isGuard=1 order by intimacy desc";
        list.add(userId);
        return this.queryList(sql,list,AppGuard.class);
    }



    public AppGuard getGuaardByUserId(Integer userId,Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = " select * from app_guard where userId=? and beiUserId=?";
        list.add(userId);
        list.add(beiUserId);
        return this.queryObj(sql,list,AppGuard.class);
    }




    /**
     * 添加守护
     * @param appGuard
     * @return
     */
    public AppGuard addGuard(AppGuard appGuard)
    {
        appGuard.setId(this.add(appGuard));
        return appGuard;
    }

    /**
     * 更新守护状态
     * @param userId
     * @param beiUserId
     * @param isGuard
     */
    public void updateGuard(Integer userId,Integer beiUserId,Integer isGuard)
    {
        ArrayList<Object> list = new ArrayList<>();
        Date date = new Date();
        String sql = "update app_guard set isGuard=?,createtime=? where userId=? and beiUserId and isGuard!=?";
        list.add(isGuard);
        list.add(date);
        list.add(userId);
        list.add(beiUserId);
        list.add(isGuard);
        this.update(sql,list);
    }


    public void updateGuardById(Integer id,Integer isGuard)
    {
        ArrayList<Object> list = new ArrayList<>();
        Date date = new Date();
        String sql = "update app_guard set isGuard=?,createtime=? where id=?";
        list.add(isGuard);
        list.add(date);
        list.add(id);
        this.update(sql,list);
    }


    public void incIntimacyById(Integer id, BigDecimal intimacy, BigDecimal giftMoney)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "update app_guard set intimacy=intimacy+?,giftMoney=giftMoney+? where id=?";
        list.add(intimacy);
        list.add(giftMoney);
        list.add(id);
        this.update(sql,list);
    }


    public List<AppGuard> getAllGuardList()
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_guard where intimacy>0 and giftMoney>0";
        return this.queryList(sql,list,AppGuard.class);
    }


    public void updateIntimacyAndGiftMoney(Integer id,BigDecimal intimacy,BigDecimal giftMoney)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "update app_guard set intimacy=intimacy-?,giftMoney=giftMoney-? where id=? and intimacy>=? and giftMoney>=?";
        list.add(intimacy);
        list.add(giftMoney);
        list.add(id);
        list.add(intimacy);
        list.add(giftMoney);
        this.update(sql,list);
    }


    public AppGuard getMaxIntimacyInfo(Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_guard where beiUserId=? and isGuard != 1 order by intimacy desc limit 1";
        list.add(beiUserId);
        return this.queryObj(sql,list,AppGuard.class);
    }


    public List<AppGuard> intimacyGuardList(Integer beiUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        AppSetup setup = appSetupDao.get();
        String sql = "select * from app_guard where beiUserId=? and intimacy>=? order by isGuard asc,intimacy desc";
        list.add(beiUserId);
        list.add(setup.getMinIntimacyShow());
        return this.queryList(sql,list,AppGuard.class);
    }



    public List<AppGuard> guardList(SplitPageBean splitPageBean,AppGuard appGuard)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_guard where isGuard=1";
        if(StringEx.isNotNull(appGuard.getUserPhoneA())){
            AppUser byLoginname = appUserDao.findByLoginname(appGuard.getUserPhoneA());
            if(byLoginname!=null){
                sql += " and userId=?";
                list.add(appGuard.getUserPhoneA());
            }
        }
        if(StringEx.isNotNull(appGuard.getUserPhoneB())){
            AppUser byLoginname = appUserDao.findByLoginname(appGuard.getUserPhoneB());
            if(byLoginname!=null){
                sql += " and beiUserId=?";
                list.add(appGuard.getUserPhoneB());
            }
        }
        sql += " order by intimacy desc,createtime desc";
        return this.queryList(sql,list,AppGuard.class,splitPageBean);
    }


    public List<AppGuard> getIntimacyList(SplitPageBean splitPageBean,Integer userId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_guard where intimacy>0 and userId=?";
        list.add(userId);
        return this.queryList(sql,list,AppGuard.class,splitPageBean);
    }



    public AppGuard isGuild(Integer userId, Integer toUserId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = " select * from app_guard where userId=? and beiUserId=? and isGuard=1";
        list.add(userId);
        list.add(toUserId);
        return this.queryObj(sql,list,AppGuard.class);
    }

    public void delByUserId(Integer userId) {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "delete from app_guard where userId=? or beiUserId=?";
        list.add(userId);
        list.add(userId);
        this.del(sql, list);
    }



}
